/*
 * @Description:
 * @Author: Evan Zhang. evan.holmes@foxmail.com
 * @Date: 2024-05-22 12:47:32
 * @LastEditTime: 2024-05-22 13:21:52
 * @LastEditors: Evan Zhang. evan.holmes@foxmail.com
 * @Reference:
 */
package main

import (
	"context"
	"fmt"
	"net"
	"os"
	"time"
)

func main() {
	domain := os.Args[1]
	ip := lookupHost(domain)
	fmt.Println("------------------------------------------------------------")
	fmt.Println("Add record to 'C:\\Windows\\System32\\drivers\\etc\\hosts'")
	fmt.Println("------------------------------------------------------------\n")
	fmt.Printf("%s %s\n\n", ip, domain)
}

func lookupHost(domain string) string {
	r := &net.Resolver{
		PreferGo: true,
		Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
			d := net.Dialer{
				Timeout: time.Millisecond * time.Duration(10000),
			}
			return d.DialContext(ctx, network, "8.8.8.8:53")
		},
	}
	ip, _ := r.LookupHost(context.Background(), domain)
	return ip[0]
}
